package com.crocodile.game17.common;

import cn.hutool.core.date.DateTime;
import lombok.Data;

/**
 * 通用返回类
 * @param <T>
 */

@Data
public class R<T> {

    //状态
    private int status;
    //是否成功
    private boolean success;
    //返回消息
    private String message;
    //返回的数据
    private T data;
    //返回的时间
    private DateTime time;

    /**
     * 正确返回 result
     * @param data
     * @param <T>
     * @return
     */
    public static <T> R<T> success(T data){
        R<T> r = new R<>();
        r.success = true;
        r.status = 200;
        r.data = data;
        r.message = "successful";
        r.time = DateTime.now();

        return r;
    }

    public static <T> R<T> success(String msg ,T data){
        R<T> r = new R<>();
        r.success = true;
        r.status = 200;
        r.data = data;
        r.message = msg;
        r.time = DateTime.now();

        return r;
    }

    /**
     * 错误 result
     * @param msg
     * @param status
     * @param <T>
     * @return
     */
    public static <T> R<T> error(String msg,int status){
        R<T> r = new R<>();
        r.success = false;
        r.status = status;
        r.message = msg;
        r.time = DateTime.now();

        return r;
    }

    public static <T> R<T> error(AppExceptionMsgEnum appExceptionMsg){
        R<T> r = new R<>();
        r.success = false;
        r.status = appExceptionMsg.getStatus();
        r.message = appExceptionMsg.getMsg();
        r.time = DateTime.now();

        return r;
    }
}
